<?php
set_include_path("../include;include");
require_once "/model/Group.php";
require_once "/model/Supplement.php";
require_once "/util/helperFunctions.php";
require_once "/dao/MemberDAO.php";

class Member {
	private $id;
	private $firstname;
	private $lastname;
	private $group;

	public function __construct() {

	}

	public static function createNew(string $firstname, string $lastname, Group $group) {
		$instance = new self();
		$instance->firstname = $firstname;
		$instance->lastname = $lastname;
		$instance->group = $group;
		return $instance;
	}

	public function save() {
		$dao = MemberDAO::getInstance();

		$this->group->save();
		$myid = $dao->save($this);
		$this->id = $myid;
		return $myid;
	}

	public function load(integer $id) {
		$dao = MemberDAO::getInstance();

		$this->id = $id;
		$this->firstname = $dao->getFirstnameByMemberId($id);
		$this->lastname = $dao->getLastnameByMemberId($id);
		$this->group = Group::loadGroup($dao->getGroupIdByMemberId($id));
	}


	/**
	 * Static save method, to save a Member object from somwhere else;
	 * @param Member $member The Member that is to be saved
	 */
	public static function saveMember(Member $member) {
		$dao = MemberDAO::getInstance();

		$event_id = $dao->save($event);
		$event->setId($event_id);
	}

	/**
	 * Static load method, to load an existing Member object from somewhere else, using its id.
	 * @param integer $id The unique id of the Member
	 */
	public static function loadMember(integer $id) {
		$member = new self();
		$member->id = $id;
		return $member->load($id);
	}


	/* Getters and setters */

	public function getId() {
		return $this->id;
	}

	public function setId(integer $id) {
		$this->id = $id;
	}

	public function getFirstname() {
		return $this->firstname;
	}

	public function setFirstname(string $firstname) {
		$this->firstname = $firstname;
	}

	public function getLastname() {
		return $this->lastname;
	}

	public function setLastname(string $lastname) {
		$this->lastname = $lastname;
	}

	public function getGroup() {
		return $this->group;
	}

	public function setGroup(Group $group) {
		$this->group = $group;
	}


	public function __toString() {
		$output = $this->firstname. " ".$this->lastname . " ";
		if (!is_null($this->group) && isset($this->group)) {
			$output .= "(".$this->group.")";
		}

		return $output;
	}

}


?>